/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include <stdint.h>
struct mcuInfo
{
    uint64_t device_id;
    char *info;
};
const struct mcuInfo mcuInfoTable[] = {
    //"403B mcu"
    {0x0000000270050240, "AT32F403ZCT6_256KB_LQFP144"},
    {0x0000000270050241, "AT32F403VCT6_256KB_LQFP100"},
    {0x0000000270050242, "AT32F403RCT6_256KB_LQFP64"},
    {0x0000000270050243, "AT32F403CCT6_256KB_LQFP48"},
    {0x0000000270050344, "AT32F403ZGT6_1024KB_LQFP144"},
    {0x0000000270050345, "AT32F403VGT6_1024KB_LQFP100"},
    {0x0000000270050346, "AT32F403RGT6_1024KB_LQFP64"},
    {0x0000000270050347, "AT32F403CGT6_1024KB_LQFP48"},
    {0x00000002700502C8, "AT32F403ZET6_512KB_LQFP144"},
    {0x00000002700502C9, "AT32F403VET6_512KB_LQFP100"},
    {0x00000002700502CA, "AT32F403RET6_512KB_LQFP64"},
    {0x00000002700502CB, "AT32F403CET6_512KB_LQFP48"},
    {0x000000027005034C, "AT32F403CGU6_1024KB_QFN48"},
    {0x00000002700502CD, "AT32F403CEU6_512KB_QFN48"},
    {0x000000027005024E, "AT32F403CCU6_256KB_QFN48"},
    {0x0000000470030240, "AT32F413RCT7_256KB_LQFP64"},
    {0x00000004700301C1, "AT32F413RBT7_128KB_LQFP64"},
    {0x0000000470030242, "AT32F413CCT7_256KB_LQFP48"},
    {0x00000004700301C3, "AT32F413CBT7_128KB_LQFP48"},
    {0x0000000470030244, "AT32F413KCU7_4_256KB_QFN32"},
    {0x00000004700301C5, "AT32F413KBU7_4_128KB_QFN32"},
    {0x0000000470030106, "AT32F413C8T7_64KB_LQFN48"},
    {0x0000000470030247, "AT32F413CCU7_256KB_QFP48"},
    {0x00000004700301C0, "AT32F413CBU7_128KB_QFP48"},
    {0x0000000570030240, "AT32F415RCT7_256KB_LQFP64"},
    {0x0000000570030241, "AT32F415CCT7_256KB_LQFP48"},
    {0x0000000570030242, "AT32F415KCU7_4_256KB_QFN32"},
    {0x0000000570030243, "AT32F415RCT7_7_256KB_LQFP64"},
    {0x00000005700301C4, "AT32F415RBT7_128KB_LQFP64"},
    {0x00000005700301C5, "AT32F415CBT7_128KB_LQFP48"},
    {0x00000005700301C6, "AT32F415KBU7_4_128KB_QFN32"},
    {0x00000005700301C7, "AT32F415RBT7_7_128KB_LQFP64"},
    {0x0000000570030108, "AT32F415R8T7_64KB_LQFP64"},
    {0x0000000570030109, "AT32F415C8T7_64KB_LQFP48"},
    {0x000000057003010B, "AT32F415R8T7_7_64KB_QFN32"},
    {0x000000057003010A, "AT32F415K8U7_4_64KB_LQFP64"},
    {0x00000005700301CD, "AT32F415CBU7_128KB_QFN48"},
    {0x000000057003024C, "AT32F415CCU7_256KB_QFN48"},
    {0x0000000770050240, "AT32F403AVCT7_256KB_LQFP100"},
    {0x0000000770050241, "AT32F403ARCT7_256KB_LQFP64"},
    {0x0000000770050242, "AT32F403ACCT7_256KB_LQFP48"},
    {0x0000000770050243, "AT32F403ACCU7_256KB_QFN48"},
    {0x0000000770050344, "AT32F403AVGT7_1024KB_LQFP100"},
    {0x0000000770050345, "AT32F403ARGT7_1024KB_LQFP64"},
    {0x0000000770050346, "AT32F403ACGT7_1024KB_LQFP48"},
    {0x0000000770050347, "AT32F403ACGU7_1024KB_QFN48"},
    {0x00000007700502CD, "AT32F403AVET7_512KB_LQFP100"},
    {0x00000007700502CE, "AT32F403ARET7_512KB_LQFP64"},
    {0x00000007700503CF, "AT32F403ACET7_512KB_LQFP48"},
    {0x00000007700503D0, "AT32F403ACEU7_512KB_QFN48"},
    {0x0000000870050249, "AT32F407VCT7_256KB_LQFP100"},
    {0x000000087005024A, "AT32F407RCT7_256KB_LQFP64"},
    {0x000000087005034B, "AT32F407VGT7_1024KB_LQFP100"},
    {0x000000087005034C, "AT32F407RGT7_1024KB_LQFP64"},
    {0x00000008700502D1, "AT32F407VET7_512KB_LQFP100"},
    {0x00000008700502D2, "AT32F407RET7_512KB_LQFP64"},
    {0x0000000950020100, "AT32F421C8T7_64KB_LQFP48"},
    {0x0000000950020101, "AT32F421K8T7_64KB_LQFP32"},
    {0x0000000950020102, "AT32F421K8U7_64KB_QFN32"},
    {0x0000000950020103, "AT32F421K8U7_4_64KB_QFN32_4X4"},
    {0x0000000950020104, "AT32F421F8U7_64KB_QFN20"},
    {0x0000000950020105, "AT32F421F8P7_64KB_TSSOP20"},
    {0x0000000950020086, "AT32F421C6T7_32KB_LQFP48"},
    {0x0000000950020087, "AT32F421K6T7_32KB_LQFP32"},
    {0x0000000950020088, "AT32F421K6U7_32KB_QFN32"},
    {0x0000000950020089, "AT32F421K6U7_4_32KB_QFN32_4x4"},
    {0x000000095002008A, "AT32F421F6U7_32KB_QFN20"},
    {0x000000095002008B, "AT32F421F6P7_32KB_TSSOP20"},
    {0x000000095001000C, "AT32F421C4T7_16KB_LQFP48"},
    {0x000000095001000D, "AT32F421K4T7_16KB_LQFP32"},
    {0x000000095001000E, "AT32F421K4U7_16KB_QFN32"},
    {0x000000095001000F, "AT32F421K4U7_4_16KB_QFN32_4x4"},
    {0x0000000950010010, "AT32F421F4U7_16KB_QFN20"},
    {0x0000000950010011, "AT32F421F4P7_16KB_TSSOP20"},
    {0x0000000950020112, "AT32F421G8U7_64KB_QFN28"},
    {0x0000000950020093, "AT32F421G6U7_32KB_QFN28"},
    {0x0000000950010014, "AT32F421G4U7_16KB_QFN28"},
    {0x0000000950020115, "AT32F421PF8P7_64KB_TSSOP20"},
    {0x0000000950010016, "AT32F421PF4P7_16KB_TSSOP20"},
    {0x0000000950020117, "AT32F421C8W_64KB"},
    {0x00000009D0020100, "AT32F421C8W_YY_64KB"},
    {0x0000000D70084540, "AT32F435ZMT7_4032KB_LQFP144"},
    {0x0000000D70083341, "AT32F435ZGT7_1024KB_LQFP144"},
    {0x0000000D70083242, "AT32F435ZCT7_256KB_LQFP144"},
    {0x0000000D70084543, "AT32F435VMT7_4032KB_LQFP100"},
    {0x0000000D70083344, "AT32F435VGT7_1024KB_LQFP144"},
    {0x0000000D70083245, "AT32F435VCT7_256KB_LQFP100"},
    {0x0000000D70084546, "AT32F435RMT7_4032KB_LQFP64"},
    {0x0000000D70083347, "AT32F435RGT7_1024KB_LQFP64"},
    {0x0000000D70083248, "AT32F435RCT7_256KB_LQFP64"},
    {0x0000000D70084549, "AT32F435CMT7_4032KB_LQFP48"},
    {0x0000000D7008334A, "AT32F435CGT7_1024KB_LQFP48"},
    {0x0000000D7008324B, "AT32F435CCT7_256KB_LQFP48"},
    {0x0000000D7008454C, "AT32F435CMU7_4032KB_QFN48"},
    {0x0000000D7008334D, "AT32F435CGU7_1024KB_QFN48"},
    {0x0000000D7008324E, "AT32F435CCU7_256KB_QFN48"},
    {0x0000000E7008454F, "AT32F437ZMT7_4032KB_LQFP144"},
    {0x0000000E70083350, "AT32F437ZGT7_1024KB_LQFP144"},
    {0x0000000E70083251, "AT32F437ZCT7_256KB_LQFP144"},
    {0x0000000E70084552, "AT32F437VMT7_4032KB_LQFP100"},
    {0x0000000E70083353, "AT32F437VGT7_1024KB_LQFP100"},
    {0x0000000E70083254, "AT32F437VCT7_256KB_LQFP100"},
    {0x0000000E70084555, "AT32F437RMT7_4032KB_LQFP64"},
    {0x0000000E70083356, "AT32F437RGT7_1024KB_LQFP64"},
    {0x0000000E70083257, "AT32F437RCT7_256KB_LQFP64"},
    {0x00000012700A3240, "AT32F423VCT7_256KB_LQFP100"},
    {0x00000012700A21C1, "AT32F423VBT7_128KB_LQFP100"},
    {0x0000001270032102, "AT32F423V8T7_64KB_LQFP100"},
    {0x00000012700A3243, "AT32F423RCT7_256KB_LQFP64"},
    {0x00000012700A21C4, "AT32F423RBT7_128KB_LQFP64"},
    {0x0000001270032105, "AT32F423R8T7_64KB_LQFP64"},
    {0x00000012700A3246, "AT32F423RCT7_7_256KB_LQFP64"},
    {0x00000012700A21C7, "AT32F423RBT7_7_128KB_LQFP64"},
    {0x0000001270032108, "AT32F423R8T7_7_64KB_LQFP64"},
    {0x00000012700A3249, "AT32F423CCT7_256KB_LQFP48"},
    {0x00000012700A21CA, "AT32F423CBT7_128KB_LQFP48"},
    {0x000000127003210B, "AT32F423C8T7_64KB_LQFP48"},
    {0x00000012700A324C, "AT32F423CCU7_256KB_QFN48"},
    {0x00000012700A21CD, "AT32F423CBU7_128KB_QFN48"},
    {0x000000127003210E, "AT32F423C8U7_64KB_QFN48"},
    {0x00000012700A3250, "AT32F423TCU7_256KB_QFN36"},
    {0x00000012700A21D1, "AT32F423TBU7_128KB_QFN36"},
    {0x0000001270032112, "AT32F423T8U7_64KB_QFN36"},
    {0x00000012700A3253, "AT32F423KCU7_4_256KB_QFN32"},
    {0x00000012700A21D4, "AT32F423KBU7_4_128KB_QFN32"},
    {0x0000001270032115, "AT32F423K8U7_4_64KB_QFN32"},
    {0x0000000F50092100, "AT32F425R8T7_64KB_64LQFP(10x10)"},
    {0x0000000F50092081, "AT32F425R6T7_32KB_64LQFP(10x10)"},
    {0x0000000F50092103, "AT32F425R8T7_7_64KB_64LQFP(7x7)"},
    {0x0000000F50092084, "AT32F425R6T7_7_32KB_64LQFP(7x7)"},
    {0x0000000F50092106, "AT32F425C8T7_64KB_48LQFP"},
    {0x0000000F50092087, "AT32F425C6T7_32KB_48LQFP"},
    {0x0000000F50092109, "AT32F425C8U7_64KB_48QFN"},
    {0x0000000F5009208A, "AT32F425C6U7_32KB_48QFN"},
    {0x0000000F5009210C, "AT32F425K8T7_64KB_32LQFP"},
    {0x0000000F5009208D, "AT32F425K6T7_32KB_32LQFP"},
    {0x0000000F5009210F, "AT32F425K8U7_4_64KB_32QFN"},
    {0x0000000F50092090, "AT32F425K6U7_4_32KB_32QFN"},
    {0x0000000F50092112, "AT32F425F8P7_64KB_20TSSOP"},
    {0x0000000F50092093, "AT32F425F6P7_32KB_20TSSOP"},
    {0x0000001870050340, "AT32A403AVGT7_1024KB_LQFP100"},
    {0x00000018700502C1, "AT32A403AVET7_512KB_LQFP100"},
    {0x0000001870050242, "AT32A403AVCT7_256KB_LQFP100"},
    {0x0000001870050343, "AT32A403ARGT7_1024KB_LQFP64"},
    {0x00000018700502C4, "AT32A403ARET7_512KB_LQFP64"},
    {0x0000001870050245, "AT32A403ARCT7_256KB_LQFP64"},
    {0x0000001870050346, "AT32A403ACGT7_1024KB_LQFP48"},
    {0x00000018700502C7, "AT32A403ACET7_512KB_LQFP48"},
    {0x0000001870050248, "AT32A403ACCT7_256KB_LQFP48"},
    {0x0000001870050349, "AT32A403ACGU7_1024KB_QFN48"},
    {0x00000018700502CA, "AT32A403ACEU7_512KB_QFN48"},
    {0x000000187005024B, "AT32A403ACCU7_256KB_QFN48"},
    {0x0000001170030250, "AT32WB415CCU7_7_256KB_QFN48_7x7"},
};
char *at_get_mcu_info(uint64_t deviceID)
{
    for (int i = 0; i < sizeof(mcuInfoTable) / sizeof(struct mcuInfo); i++)
    {
        if (deviceID == mcuInfoTable[i].device_id)
        {
            return mcuInfoTable[i].info;
        }
    }
    return 0;
}
uint32_t at32_get_flash_size(uint64_t DeviceID)
{
    uint32_t size = 0;
    uint8_t sizeCode = (DeviceID >> 4) & 0xFF;
    switch (sizeCode)
    {
    case 0x01:
        size = 16 * 1024;
        break;
    case 0x08:
        size = 32 * 1024;
        break;
    case 0x10:
        size = 64 * 1024;
        break;
    case 0x1C:
        size = 128 * 1024;
        break;
    case 0x24:
        size = 256 * 1024;
        break;
    case 0x2C:
        size = 512 * 1024;
        break;
    case 0x34:
        size = 1024 * 1024;
        break;
    case 0x54:
        size = 4032 * 1024;
        break;
    default:
        size = 0;
        break;
    }
    return size;
}
